/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.supply;

import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.TextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import main.WinkelApplication;
import model.Category;
import model.Product;

/**
 *
 * @author ShalDDA
 */
public class SupplyAdd extends JPanel{
    private ArrayList<String> catIdArrayText;
    private JComboBox ComboCat;
    private TextField naamText;
    private TextField prijsText;
    private TextField omschrijvingText;
    private TextField voorraadText;
    private int catId;
   
    SupplyAdd() {
        super();
        toSetLayout();
    }

    private void toSetLayout() {
        setLayout(new BorderLayout());
        add(getTitle(), BorderLayout.PAGE_START);
        //???????????????????????????????????????????????????????????????????????? Component pnlCompenents;
        
        add(pnlCompenents(), BorderLayout.CENTER);
        add(getPnlButtons(), BorderLayout.PAGE_END);
    }

    private JLabel getTitle() {
        JLabel title = new JLabel("Product toevoegen", SwingConstants.CENTER);
        title.setFont(WinkelApplication.computeFontSize(2));
        
        return title;
    }

    private JPanel pnlCompenents() {
        FormLayout layout = new FormLayout(
        "right:max(40dlu;pref), 3dlu, 70dlu, 7dlu, " +
        "right:max(40dlu;pref), 3dlu, 70dlu",
        "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +
        "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +
        "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +
        "p, 3dlu, p, 3dlu, p, 3dlu, p");
                
        JPanel panel = new JPanel(layout);
        panel.setBorder(Borders.DIALOG_BORDER);
        
        List<Category> catIdList = WinkelApplication.getQueryManager().getCategories();
        String[] catArray = new String[catIdList.size()];
        
        
        
        for(int i = 0; i < catIdList.size(); i++) {
            Category category = catIdList.get(i);
            catArray[category.getCategoryId()-1] = category.getName();
           // ComboCat.addItem(category);
            //catIdArrayText[i] = category.getName();
        }
        
        ComboCat = new JComboBox(catArray);
        
        ComboCat.setSelectedIndex(0);
        naamText = new TextField();
        prijsText = new TextField();
        omschrijvingText = new TextField();
        voorraadText = new TextField();
        
        JLabel catId = new JLabel("Catergorie-ID");
        JLabel naam = new JLabel("Naam");
        JLabel prijs = new JLabel("Prijs");
        JLabel omschrijving = new JLabel("Omschrijving");
        JLabel voorraad = new JLabel("Voorraad");
        
        catId.setFont(WinkelApplication.computeFontSize(3));
        naam.setFont(WinkelApplication.computeFontSize(3));
        prijs.setFont(WinkelApplication.computeFontSize(3));
        omschrijving.setFont(WinkelApplication.computeFontSize(3));
        voorraad.setFont(WinkelApplication.computeFontSize(3));
        
        
        // Fill the table with labels and components.
        CellConstraints cc = new CellConstraints();
        
        panel.add(catId,             cc.xy (1, 3));
        panel.add(ComboCat,         cc.xyw(3, 3, 3));
        panel.add(naam,              cc.xy (1, 5));
        panel.add(naamText,          cc.xyw(3, 5, 3));
        panel.add(prijs,             cc.xy (1, 7));
        panel.add(prijsText,         cc.xyw(3, 7, 3));
        panel.add(omschrijving,      cc.xy (1, 9));
        panel.add(omschrijvingText,  cc.xyw(3, 9, 3)); 
        panel.add(voorraad,          cc.xy(1, 11));
        panel.add(voorraadText,      cc.xyw(3, 11, 3));
    
        
        /*JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        JLabel catId = new JLabel("Catergorie-ID");
        catId.setFont(WinkelApplication.computeFontSize(3));
        
        c.gridx = 0;
        c.gridy = 0;
        panel.add(catId);
        
        JLabel naam = new JLabel("Naam");
        naam.setFont(WinkelApplication.computeFontSize(3));
        
        c.gridx = 0;
        c.gridy = 1;
        panel.add(naam, c);
        
        JLabel prijs = new JLabel("Prijs");
        prijs.setFont(WinkelApplication.computeFontSize(3));
        
        c.gridx = 0;
        c.gridy = 2;
        panel.add(naam, c);
        
        JLabel omschrijving = new JLabel("Omschrijving");
        omschrijving.setFont(WinkelApplication.computeFontSize(3));
        
        c.gridx = 0;
        c.gridy = 3;
        panel.add(omschrijving, c);
        
        JLabel voorraad = new JLabel("Voorraad");
        voorraad.setFont(WinkelApplication.computeFontSize(3));
        
        c.gridx = 0;
        c.gridy = 4;
        panel.add(voorraad, c);
         * 
         */
        
        return panel;      
    }

    private Component getPnlButtons() {
        JPanel panel = new JPanel();
        
        JButton terug = new JButton("Annuleren");
        
        terug.setPreferredSize(new Dimension(150,25));
        terug.addMouseListener(new MouseAdapter() {
          
          @Override
          public void mouseClicked(MouseEvent e) {
            javax.swing.SwingUtilities.getWindowAncestor(SupplyAdd.this).dispose();
          }
        });
        
        JButton opslaan = new JButton("Opslaan");
        
        opslaan.setPreferredSize(new Dimension(150,25));
        opslaan.addMouseListener(new MouseAdapter() {
           
          
          @Override
          public void mouseClicked(MouseEvent e) {
            
           // Category item = (Category) ComboCat.getSelectedItem(); //Integer.parseInt(ComboCat.selectedItemReminder);.
            int cat_id = ComboCat.getSelectedIndex()+1; //.getCategoryId();
            
            double prijs = Double.parseDouble(prijsText.getText());
            int voorraad = Integer.parseInt(voorraadText.getText());

            addProduct( cat_id, naamText.getText(),
                        omschrijvingText.getText(), prijs, 
                        voorraad );       
          }

            private void addProduct(int id, String naam, String omschrijv, double prijs,  int voorraad) {
                Product product = new Product(id, naam, omschrijv, prijs, voorraad );
                
                
                WinkelApplication.getQueryManager().addProduct(product);

                javax.swing.SwingUtilities.getWindowAncestor(SupplyAdd.this).dispose();
                WinkelApplication.getInstance().showPanel(new view.supply.SupplyPanel());
            }
        });
        
        panel.add(opslaan);    
        panel.add(terug);
        
        return panel;
    
    }
    
}
